import { createRouter, createWebHistory } from "vue-router";

// 导入视图组件
import Home from "@/views/home/index.vue";
import NotFound from "@/views/NotFound.vue";

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes: [
    // 404 页面
    {
      path: "/:pathMatch(.*)*",
      name: "NotFound",
      component: NotFound,
      meta: {
        title: "页面未找到 - Anytify",
      },
    },
    {
      path: "/login",
      name: "Login",
      component: () => import("@/views/login.vue"),
      meta: {
        title: "登录 - Anytify",
      },
    },
    {
      path: "/register",
      name: "Register",
      component: () => import("@/views/register.vue"),
      meta: {
        title: "注册 - Anytify",
      },
    },
    // 首页
    {
      path: "/",
      name: "Home",
      redirect: "/index",
      children: [
        {
          path: "/index",
          name: "Index",
          component: Home,
          meta: {
            title: "首页 - Anytify",
          },
        },
        {
          path: "/index/version",
          name: "Version",
          component: () => import("@/views/home/version.vue"),
          meta: {
            title: "专业版本 - Anytify",
          },
        },
        {
          path: "/index/secure",
          name: "Secure",
          component: () => import("@/views/home/secure.vue"),
          meta: {
            title: "安全 - Anytify",
          },
        },
        {
          path: "/index/team",
          name: "Team",
          component: () => import("@/views/home/team.vue"),
          meta: {
            title: "商务团队 - Anytify",
          },
        },
        {
          path: "/index/career",
          name: "Career",
          component: () => import("@/views/home/career.vue"),
          meta: {
            title: "职业岗位 - Anytify",
          },
        },
        {
          path: "/index/callme",
          name: "Callme",
          component: () => import("@/views/home/callme.vue"),
          meta: {
            title: "联系我们 - Anytify",
          },
        },
      ],
    },
    // 工作界面
    {
      path: "/work",
      name: "Work",
      component: () => import("@/views/work/index.vue"),
      meta: {
        title: "工作界面 - Anytify",
      },
    },
    // 探索
    {
      path: "/seek",
      name: "Seek",
      redirect: "/seek/import",
      meta: {
        title: "探索 - Anytify",
      },
      children: [
        {
          path: "/seek/import",
          name: "Import",
          component: () => import("@/views/seek/import.vue"),
          meta: {
            title: "导入CAD文件 - Anytify",
          },
        },
        {
          path: "/seek/3DConfig",
          name: "3DConfig",
          component: () => import("@/views/seek/3DConfig.vue"),
          meta: {
            title: "3D配置器制造商 - Anytify",
          },
        },
        {
          path: "/seek/shareImplant",
          name: "ShareImplant",
          component: () => import("@/views/seek/shareImplant.vue"),
          meta: {
            title: "共享和嵌入3D文件 - Anytify",
          },
        },
        {
          path: "/seek/interact",
          name: "Interact",
          component: () => import("@/views/seek/interact.vue"),
          meta: {
            title: "交互和动画 - Anytify",
          },
        },
        {
          path: "/seek/ar",
          name: "AR",
          component: () => import("@/views/seek/ar.vue"),
          meta: {
            title: "增强现实 - Anytify",
          },
        },
        {
          path: "/seek/figma",
          name: "Figma",
          component: () => import("@/views/seek/figma.vue"),
          meta: {
            title: "Figma集成 - Anytify",
          },
        },
      ],
    },
    // 商业
    {
      path: "/business",
      name: "Business",
      redirect: "/business/businessVersion",
      meta: {
        title: "商业 - Anytify",
      },
      children: [
        {
          path: "/business/businessVersion",
          name: "BusinessVersion",
          component: () => import("@/views/business/businessVersion.vue"),
          meta: {
            title: "商业版 - Anytify",
          },
        },
        {
          path: "/business/workspace",
          name: "BusinessWorkspace",
          component: () => import("@/views/business/workspace.vue"),
          meta: {
            title: "工作区 - Anytify",
          },
        },
        {
          path: "/business/api",
          name: "BusinessApi",
          component: () => import("@/views/business/api.vue"),
          meta: {
            title: "API - Anytify",
          },
        },
        {
          path: "/business/case",
          name: "BusinessCase",
          component: () => import("@/views/business/case.vue"),
          meta: {
            title: "案例 - Anytify",
          },
        },
        {
          path: "/business/support",
          name: "BusinessSupport",
          component: () => import("@/views/business/support.vue"),
          meta: {
            title: "客户支持 - Anytify",
          },
        },
        {
          path: "/business/contact",
          name: "BusinessContact",
          component: () => import("@/views/business/contact.vue"),
          meta: {
            title: "业务联系 - Anytify",
          },
        },
      ],
    },
    //  资源
    {
      path: "/resources",
      name: "Resources",
      redirect: "/resources/3d",
      meta: {
        title: "资源 - Anytify",
      },
      children: [
        {
          path: "/resources/3d",
          name: "ThreeD",
          component: () => import("@/views/resources/threeD.vue"),
          meta: {
            title: "3D配置器示例 - Anytify",
          },
        },
        {
          path: "/resources/successCases",
          name: "SuccessCases",
          component: () => import("@/views/resources/successCases.vue"),
          meta: {
            title: "成功案例 - Anytify",
          },
        },
        {
          path: "/resources/forum",
          name: "Forum",
          component: () => import("@/views/resources/forum.vue"),
          meta: {
            title: "论坛 - Anytify",
          },
        },
        {
          path: "/resources/document",
          name: "Document",
          component: () => import("@/views/resources/document.vue"),
          meta: {
            title: "文档 - Anytify",
          },
        },
      ],
    },
    // 定价
    {
      path: "/price",
      name: "Price",
      component: () => import("@/views/price/index.vue"),
      meta: {
        title: "定价 - Anytify",
      },
    },
    // 大赛
    {
      path: "/competition",
      name: "Competition",
      redirect: "/competition/hainan/index", // 默认跳转海南活动
      meta: {
        title: "《山海逐梦》全国商战电视真人秀大型励志创业大赛",
      },
      children: [
        {
          path: "/competition/hainan/index",
          name: "hainanIndex",
          meta: {
            title: "《山海逐梦》全国商战电视真人秀大型励志创业大赛——海南地方海选赛",
          },
          component: () => import("@/views/competition/hainan/index.vue"),
        },
        {
          path: "/competition/hainan/signUp",
          name: "hainanSignUp",
          meta: {
            title: "《山海逐梦》全国商战电视真人秀大型励志创业大赛——海南地方海选赛",
          },
          component: () => import("@/views/competition/hainan/signUp.vue"),
        },
      ]
    },
  ],
  // 平滑滚动到顶部
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0, behavior: "smooth" };
    }
  },
});

// 全局前置守卫 - 设置页面标题
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title || "Anytify";
  next();
});

export default router;
